package com.example.springboot1.controller;

import com.example.springboot1.entity.User;
import com.example.springboot1.service.Loginservice;
import com.example.springboot1.service.Registerservice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@Controller
public class LoginController {
    @Autowired
    Loginservice loginservice;
    @Autowired
    Registerservice registerservice;
    @RequestMapping(value = "/userlogin")
    public ModelAndView userlogin(ModelAndView modelAndView) {
        log.info("欢迎进入登陆界面");
        User user = new User();
        modelAndView.addObject("user",user);
        modelAndView.setViewName("login");
        return modelAndView;
    }
    @RequestMapping(value = "/login_in")
    public ModelAndView login_in(ModelAndView modelAndView,User user) {
        if(!(loginservice.isusernameexists(user))){
            modelAndView.setViewName("index");
        }
        else{
            if(loginservice.login(user.getUsername(),user.getPassword())){
                modelAndView.addObject("user",user);
            modelAndView.setViewName("login_success");
        }
        else {
            modelAndView.addObject("error","登陆失败");
            modelAndView.setViewName("login");
        }
        }
        return modelAndView;
    }
}
